package com.ruoyi.functiontest.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.functiontest.mapper.TollBridgeStatTestMapper;
import com.ruoyi.functiontest.domain.TollBridgeStatTest;
import com.ruoyi.functiontest.service.ITollBridgeStatTestService;
import com.ruoyi.common.utils.StringUtils;

/**
 * 收费桥梁统计测试Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class TollBridgeStatTestServiceImpl implements ITollBridgeStatTestService 
{
    @Autowired
    private TollBridgeStatTestMapper tollBridgeStatTestMapper;

    /**
     * 查询收费桥梁统计测试
     * 
     * @param id 收费桥梁统计测试主键
     * @return 收费桥梁统计测试
     */
    @Override
    public TollBridgeStatTest selectTollBridgeStatTestById(Long id)
    {
        return tollBridgeStatTestMapper.selectTollBridgeStatTestById(id);
    }

    /**
     * 查询收费桥梁统计测试列表
     * 
     * @param tollBridgeStatTest 收费桥梁统计测试
     * @return 收费桥梁统计测试
     */
    @Override
    public List<TollBridgeStatTest> selectTollBridgeStatTestList(TollBridgeStatTest tollBridgeStatTest)
    {
        return tollBridgeStatTestMapper.selectTollBridgeStatTestList(tollBridgeStatTest);
    }

    /**
     * 新增收费桥梁统计测试
     * 
     * @param tollBridgeStatTest 收费桥梁统计测试
     * @return 结果
     */
    @Override
    public int insertTollBridgeStatTest(TollBridgeStatTest tollBridgeStatTest)
    {
        return tollBridgeStatTestMapper.insertTollBridgeStatTest(tollBridgeStatTest);
    }

    /**
     * 修改收费桥梁统计测试
     * 
     * @param tollBridgeStatTest 收费桥梁统计测试
     * @return 结果
     */
    @Override
    public int updateTollBridgeStatTest(TollBridgeStatTest tollBridgeStatTest)
    {
        return tollBridgeStatTestMapper.updateTollBridgeStatTest(tollBridgeStatTest);
    }

    /**
     * 批量删除收费桥梁统计测试
     * 
     * @param ids 需要删除的收费桥梁统计测试主键
     * @return 结果
     */
    @Override
    public int deleteTollBridgeStatTestByIds(Long[] ids)
    {
        return tollBridgeStatTestMapper.deleteTollBridgeStatTestByIds(ids);
    }

    /**
     * 删除收费桥梁统计测试信息
     * 
     * @param id 收费桥梁统计测试主键
     * @return 结果
     */
    @Override
    public int deleteTollBridgeStatTestById(Long id)
    {
        return tollBridgeStatTestMapper.deleteTollBridgeStatTestById(id);
    }
    
    /**
     * 根据ID查询是否有子节点
     * 
     * @param id 节点ID
     * @return 结果
     */
    @Override
    public boolean hasChildById(Long id)
    {
        return tollBridgeStatTestMapper.hasChildById(id) > 0;
    }

    /**
     * 批量更新实测单位审核状态（review1字段设置为0）
     * 
     * @return 结果
     */
    @Override
    public int updateReview1ToZero()
    {
        return tollBridgeStatTestMapper.updateReview1ToZero();
    }

    /**
     * 检查所有记录的review1字段是否为0
     * 
     * @return 结果
     */
    @Override
    public boolean checkAllReview1IsZero()
    {
        return tollBridgeStatTestMapper.countReview1NotZero() == 0;
    }

    /**
     * 根据条件批量更新业主审核状态（review2字段设置为0）
     * 
     * @param tollBridgeStatTest 查询条件
     * @return 结果
     */
    @Override
    public int updateReview2ToZeroByCondition(TollBridgeStatTest tollBridgeStatTest)
    {
        return tollBridgeStatTestMapper.updateReview2ToZeroByCondition(tollBridgeStatTest);
    }

    /**
     * 检查所有记录的review1和review2字段是否都为0
     * 
     * @return 结果
     */
    @Override
    public boolean checkAllReview1AndReview2AreZero()
    {
        return tollBridgeStatTestMapper.countReview1OrReview2NotZero() == 0;
    }

    /**
     * 批量更新中心审核状态（review3字段设置为0）
     * 
     * @return 结果
     */
    @Override
    public int updateReview3ToZero()
    {
        return tollBridgeStatTestMapper.updateReview3ToZero();
    }

    /**
     * 导入收费桥梁统计测试数据
     * 
     * @param tollBridgeStatTestList 收费桥梁统计测试列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importTollBridgeStatTest(List<TollBridgeStatTest> tollBridgeStatTestList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(tollBridgeStatTestList) || tollBridgeStatTestList.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        
        for (TollBridgeStatTest tollBridgeStatTest : tollBridgeStatTestList)
        {
            try
            {
                // 验证数据是否有效
                if (StringUtils.isNull(tollBridgeStatTest.getRoadName()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、路段名称不能为空");
                    continue;
                }
                if (StringUtils.isNull(tollBridgeStatTest.getNodeName()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、桥梁名称不能为空");
                    continue;
                }
                
                // 通过id和parentid组合查询，检查数据是否已存在
                TollBridgeStatTest query = new TollBridgeStatTest();
                query.setId(tollBridgeStatTest.getId());
                query.setParentId(tollBridgeStatTest.getParentId());
                query.setRoadName(tollBridgeStatTest.getRoadName());
                query.setNodeName(tollBridgeStatTest.getNodeName());
                
                List<TollBridgeStatTest> existingList = tollBridgeStatTestMapper.selectTollBridgeStatTestByIdAndParentId(query);
                
                if (StringUtils.isNotEmpty(existingList))
                {
                    // 数据已存在
                    if (isUpdateSupport)
                    {
                        TollBridgeStatTest existing = existingList.get(0);
                        tollBridgeStatTest.setId(existing.getId());
                        tollBridgeStatTestMapper.updateTollBridgeStatTest(tollBridgeStatTest);
                        successNum++;
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、数据已存在");
                    }
                }
                else
                {
                    // 数据不存在，新增数据
                    // 如果id为空，则使用自增id
                    if (StringUtils.isNull(tollBridgeStatTest.getId()))
                    {
                        tollBridgeStatTestMapper.insertTollBridgeStatTest(tollBridgeStatTest);
                    }
                    else
                    {
                        // 如果指定了id，检查id是否已存在
                        TollBridgeStatTest existingById = tollBridgeStatTestMapper.selectTollBridgeStatTestById(tollBridgeStatTest.getId());
                        if (StringUtils.isNotNull(existingById))
                        {
                            if (isUpdateSupport)
                            {
                                tollBridgeStatTestMapper.updateTollBridgeStatTest(tollBridgeStatTest);
                                successNum++;
                            }
                            else
                            {
                                failureNum++;
                                failureMsg.append("<br/>" + failureNum + "、ID已存在");
                            }
                        }
                        else
                        {
                            tollBridgeStatTestMapper.insertTollBridgeStatTest(tollBridgeStatTest);
                            successNum++;
                        }
                    }
                    successNum++;
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
